<template>
    <div class="view-wrapper">
        <div class="main" v-loading="loading">
            <!-- 基本信息 -->
            <div class="main_info">
                <p class="title_box">基础信息</p>
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm" :disabled="true">
                    <el-form-item label="所属商户：" prop="dyShopAccountId">
                        <dict-tag :options="merchantLs" :value="ruleForm.dyShopAccountId" />
                    </el-form-item>
                    <el-form-item label="商品品类：" prop="categoryValue">
                        <el-cascader v-model="ruleForm.categoryValue" :options="categoryOption" :style="{ width: inputWidth }" :props="{ value: 'id',label: 'label',children: 'children'}" filterable @change="changeCategory"></el-cascader>
                    </el-form-item>
                    <el-form-item label="商品类型：" prop="productType">
                        <dict-tag :options="commodityTypeOption" :value="ruleForm.productType" />
                    </el-form-item>
                    <div>
                        <el-form-item label="适用门店：" prop="shopList">
                            <el-table :data="tabelList" stripe border>
                                <el-table-column label="门店地址" align="center" prop="value"
                                    :show-overflow-tooltip="true" />
                                <el-table-column label="门店地址" align="center" prop="additionalValue"
                                    :show-overflow-tooltip="true" />
                                <el-table-column label="收款方式" align="center" prop="settleType"
                                    :show-overflow-tooltip="true" />
                            </el-table>
                        </el-form-item>
                        <el-form-item label="收款方式：" prop="">
                            <dict-tag :options="radioList" :value="ruleForm.settleType" />
                        </el-form-item>
                        <el-form-item label="建议用餐人数：" prop="diningNum">
                            <span v-if="ruleForm.recPersonNum==ruleForm.recPersonNumMax">{{ ruleForm.recPersonNum }}人</span>
                            <span v-else>{{ ruleForm.recPersonNum }}至{{ ruleForm.recPersonNumMax }}人</span>
                        </el-form-item>
                        <el-form-item label="商品搭配：" prop="">
                            <div class="from_detail_box" v-for="(fatherItem, fatherIndex) in ruleForm.goodsGroupList" :key="fatherIndex">
                                <el-row>
                                    <el-col :span="10" >
                                        <span>{{ fatherItem.groupName }}（{{fatherItem.totalCount}}选{{fatherItem.optionCount}}）</span>
                                    </el-col>
                                </el-row>
                                <div v-for="(soneItem, sonIndex) in fatherItem.itemList" :key="sonIndex"
                                    class="from_detail_box_son">
                                    <el-row>
                                      <el-col :span="12">
                                        <span>{{soneItem.name}}</span>
                                      </el-col>
                                      <el-col :span="5">
                                        <span>{{ soneItem.count }}{{ soneItem.unit }}</span>
                                      </el-col>
                                      <el-col :span="7">
                                        <span>{{soneItem.price}}元</span>
                                      </el-col>
                                    </el-row>
                                </div>
                            </div>
                            <div class="from_detail_bottom">
                                总价值(用户侧划线价)：{{ ruleForm.originAmount }}元
                            </div>
                        </el-form-item>
                        <el-form-item label="商品名称：" prop="productName">
                            <span>{{ ruleForm.productName }}</span>
                        </el-form-item>
                        <el-form-item label="套餐图片:" prop="setMealImgList" ref="setMealImgRef">
                            <el-image v-for="(item, indexImg) in ruleForm.setMealImgList" :key="indexImg" class="img_box" :src="item" :preview-src-list="ruleForm.setMealImgList"/>
                        </el-form-item>
                        <el-form-item label="菜品图:" prop="" ref="imgRef">
                            <el-image v-for="(item, indexImg) in ruleForm.dishesImgList" :key="indexImg" class="img_box" :src="item" :preview-src-list="ruleForm.dishesImgList"/>
                        </el-form-item>
                        <el-form-item label="环境图:" prop="" ref="imgRef">
                            <el-image v-for="(item, indexImg) in ruleForm.circumstancesImgList" :key="indexImg" class="img_box" :src="item" :preview-src-list="ruleForm.circumstancesImgList"/>
                        </el-form-item>
                        <el-form-item label="赠品信息：" prop="">
                            <div class="from_detail_box" style="padding: 0 10px 10px;">
                                <el-form-item label="是否包含赠品：" prop="freebieType">
                                    <span>{{ ruleForm.freebieType=='1'?'包含':'不包含' }}</span>
                                </el-form-item>
                                <template v-if="ruleForm.freebieType=='1'">
                                    <el-form-item label="赠品名称：" prop="">
                                        <span>{{ ruleForm.freebieName }}</span>
                                    </el-form-item>
                                    <el-form-item label="赠品库存状态：" prop="">
                                        <span>{{ ruleForm.freebieStockType=='1'?'有库存':'无库存' }}</span>
                                    </el-form-item>
                                    <el-form-item label="赠品预留库存总量：" prop="">
                                        <span>{{ ruleForm.freebieStockCount }}件</span>
                                    </el-form-item>
                                    <el-form-item label="赠品活动区：" prop="">
                                        <span>与商品可消费日期一致</span>
                                    </el-form-item>
                                    <el-form-item label="兑换规则：" prop="">
                                        <span>{{ ruleForm.freebieExchangeRule }}</span>
                                    </el-form-item>
                                </template>
                            </div>
                        </el-form-item>
                        <!-- 售卖信息 -->
                        <p class="title_box">售卖信息</p>
                        <el-form-item label="商品总价(用户侧划线价)：" prop="originAmount" v-if="ruleForm.goodsCollocationType=='2'">
                            <span>{{ ruleForm.originAmount }}</span>
                        </el-form-item>
                        <el-form-item label="顾客实际需支付：" prop="actualAmount">
                            <span>{{ ruleForm.actualAmount }}元</span>
                        </el-form-item>
                        <el-form-item label="库存数量：" prop="">
                            <span>{{ ruleForm.stockLimitType=='1'?'限库存':'不限库存' }}</span>
                        </el-form-item>
                        <el-form-item label="" prop="stockCount" v-if="ruleForm.stockLimitType=='1'">
                            <span>库存数量：{{ ruleForm.stockCount }}份</span>
                        </el-form-item>
                        <el-form-item label="投放渠道：" prop="">
                            <span>{{ ruleForm.showChannel=='1'?'不限制':'仅直播间售卖' }}</span>
                        </el-form-item>
                        <el-form-item label="商品售卖日期：" prop="">
                            <span>限时售卖</span>
                        </el-form-item>
                        <template v-if="ruleForm.soldTimeType=='1'">
                            <el-form-item label="" prop="dateSales">
                                <span>售卖日期：{{ ruleForm.soldStartTime }} 至 {{ ruleForm.soldEndTime }}</span>
                            </el-form-item>
                        </template>
                        <!-- 购买规则 -->
                        <p class="title_box">购买规则</p>
                        <el-form-item label="履约方式：" prop="">
                            <span>到店核销</span>
                        </el-form-item>
                        <el-form-item label="限购规则：" prop="">
                            <span v-if="ruleForm.buyLimitType=='1'">每人最多购买：{{ruleForm.buyLimitAmount}}份</span>
                            <span v-else>不限制购买数量</span>
                        </el-form-item>
                        <!-- 使用规则 -->
                        <p class="title_box">使用规则</p>
                        <el-form-item label="可用日期：" prop="">
                            <span v-if="ruleForm.useDateType=='2'">自购买次日起{{ ruleForm.useDateDay }}天，购买当日默认可用</span>
                            <span v-if="ruleForm.useDateType=='1'">{{ ruleForm.useStartDate }} 至 {{ ruleForm.useEndDate }} 可用</span>
                        </el-form-item>
                        <el-form-item label="不可用日期：" prop="" ref="useNotWeekRef" key="11">
                            <div class="margin_box" v-if="ruleForm.useNotWeekType=='1'">
                                <span>每</span>
                                <span v-for="(item,wIndex) in ruleForm.useNotWeek" :key="wIndex">周{{ item }}，</span>
                                <span>不可用</span>
                            </div>
                            <div class="margin_box" v-if="ruleForm.useNotDateType=='1'">
                                <span v-for="(item,dIndex) in ruleForm.useNotDateList" :key="dIndex">{{ item }}，</span>
                                <span>不可用</span>
                            </div>
                            <div v-else>--</div>
                        </el-form-item>
                        <el-form-item label="每日可用时段：" prop="" v-if="ruleForm.useTimeType=='1'">
                            <span>营业时间可用</span>
                        </el-form-item>
                        <el-form-item label="每日可用时段：" prop="" v-if="ruleForm.useTimeType=='2'">
                            <div class="from_detail_box">
                                <div v-for="(item,dIndex) in ruleForm.useTimeList" :key="dIndex" style="margin-bottom: 20px;">
                                    <span>{{ item.useStartTime }}</span>
                                    <span style="margin: 0 10px;">-</span>
                                    <span v-if="item.endTimeIsNextDay=='1'">次日</span>
                                    <span>{{ item.useEndTime }}</span>
                                </div>
                            </div>
                        </el-form-item>
                        <el-form-item label="限制使用张数：" prop="">
                            <span v-if="ruleForm.useLimitType=='2'">每人限制使用{{ ruleForm.useLimitAmount }}张</span>
                            <span v-else>不限制使用张数</span>
                        </el-form-item>
                        <el-form-item label="预约规则：" prop="">
                            <span v-if="ruleForm.appointmentType=='2'">无需预约</span>
                            <div v-if="ruleForm.appointmentType=='1'">
                                <span>需提前{{ ruleForm.appointmentNum }}</span>
                                <span v-if="ruleForm.appointmentDateType=='1'">天</span>
                                <span v-if="ruleForm.appointmentDateType=='2'">小时</span>
                                <span v-if="ruleForm.appointmentDateType=='3'">分钟</span>
                                <span>电话预约</span>
                            </div>
                        </el-form-item>
                        <el-form-item label="其他规则：" prop="">
                            <p v-if="ruleForm.superimposedDiscountsType=='1'">可与店内其他优惠同享</p>
                            <p v-if="ruleForm.superimposedDiscountsType=='2'">不可与店内其他优惠同享</p>
                            <p v-if="ruleForm.privateRoomType=='1'">可以使用包间</p>
                            <p v-if="ruleForm.privateRoomType=='2'">不可以使用包间</p>
                        </el-form-item>
                        <el-form-item label="堂食外带约定：" prop="" required>
                            <span v-if="ruleForm.freePackType=='1'">仅堂食</span>
                            <span v-if="ruleForm.freePackType=='2'">仅外带</span>
                            <span v-if="ruleForm.freePackType=='3'">支持堂食和外带</span>
                        </el-form-item>
                        <el-form-item label="其他说明信息：" prop="" v-if="ruleForm.noteList.length!=0">
                            <el-input type="textarea" :rows="5" placeholder="请输入其他规则限制，规则需与其他商品信息前后一致，多条规则请用回车换行区分" maxlength="1000" show-word-limit
                                style="width: 800px;" v-model="ruleForm.noteListStr">
                            </el-input>
                        </el-form-item>
                        <el-form-item label="退款规则：" prop="">
                            <span>到店核销:随时可退，过期未核销自动退</span>
                        </el-form-item>
                    </div>
                </el-form>
            </div>
        </div>
    </div>
</template>
<script>
const peopleLsOne = [
    { id: '1', value: '1人' }, { id: '2', value: '2人' }, { id: '3', value: '3人' }, { id: '4', value: '4人' },
    { id: '5', value: '5人' }, { id: '6', value: '6人' }, { id: '7', value: '7人' }, { id: '8', value: '8人' },
    { id: '9', value: '9人' }, { id: '10', value: '10人' }, { id: '11', value: '11人' }, { id: '12', value: '12人' },
    { id: '13', value: '13人' }, { id: '14', value: '14人' }, { id: '15', value: '15人' }, { id: '16', value: '16人' },
    { id: '17', value: '17人' }, { id: '18', value: '18人' }, { id: '19', value: '19人' }, { id: '20', value: '20人' },
]
const peopleLsTwo = [
    { id: '2', value: '2人' }, { id: '3', value: '3人' }, { id: '4', value: '4人' },
    { id: '5', value: '5人' }, { id: '6', value: '6人' }, { id: '7', value: '7人' }, { id: '8', value: '8人' },
    { id: '9', value: '9人' }, { id: '10', value: '10人' }, { id: '11', value: '11人' }, { id: '12', value: '12人' },
    { id: '13', value: '13人' }, { id: '14', value: '14人' }, { id: '15', value: '15人' }, { id: '16', value: '16人' },
    { id: '17', value: '17人' }, { id: '18', value: '18人' }, { id: '19', value: '19人' }, { id: '20', value: '20人' },
]
import {
    douyinMerchant,douyinCategory,douyinPoi,saveTradeGoods,saveTradeGoodsDraft,goodsDetail,editGoods,
    editGoodsDraft,
} from "@/api/consumeMiningManagement/dyGroupBuying";

export default {
    name: "addActive",
    components: {

    },
    data() {
        return {
            inputWidth: '300px',
            shopDisabled: false,
            loading: false,
            dialogVisible: false,
            sumitLoading: false,
            options: [],
            changeIndex: 0,
            tableData: [], //产品数组
            productLs: [], //产品详情
            step: 1,//步骤
            auditStatus:'',//审核状态 1 审核中 2 审核通过 3 审核失败 4草稿
            ruleForm: {
                actualAmount: '',//顾客实际支付价格 单位:元
                appointmentDateType: '1',//预约时间类型 1 天 2 小时 3 分钟
                appointmentNum: '',//预约时间数
                appointmentType: '2',//预约开关 1 开启 2关闭
                buyLimitAmount: '',//限购数量
                buyLimitType: '2',//限购类型 1 限购 2 不限购
                categoryIdOne: '',//一级类目id
                categoryIdThree: '',//三级类目id
                categoryIdTwo: '',//二级类目id
                circumstancesImgList: [],//环境图
                dishesImgList: [],//菜品图
                dyShopAccountId: '',//抖音生活服务商家账户id
                freePackType: '3',//堂食外带约定 1仅堂食 2仅外带 3支持堂食和外带
                freebieExchangeRule: '',//赠品兑换规则
                freebieName: '',//赠品名称
                freebieStockCount: '',//赠品库存总量
                freebieStockType: '1',//赠品库存类型 1有库存 2无库存
                freebieType: '2',//赠品包含类型 1包含 2不包含
                goodsCollocationType: '1',//商品搭配类型 1 设置单品价格，自动计算划线价 2 设置划线价，无需设置单品价格
                goodsGroupList: [ //商品搭配
                    {
                        groupName: '',//组名
                        optionCount: '',//菜品选择数量
                        totalCount: '',//商品组菜单总数
                        numSelectId:'',//几选几
                        itemList: [
                            {
                                name: '',//菜名
                                count: '',//总数
                                unit: '份',//单位 例：“份”
                                price: '',//单价
                            }
                        ],
                    }
                ],
                id: '',
                noteList: [],//其他说明
                noteListStr:'',//其他说明字符串
                originAmount: 0,//商品原价（商品划线价）单位：元
                privateRoomType: '2',//包间使用类型 1 使用 2不使用
                productName: '',//商品名称
                productType: '1',//商品类型 1 团购券
                recPersonNum: '',//建议用餐人数
                recPersonNumMax: '',//建议用餐最大人数
                setMealImgList: [],//套餐图
                shopList: [],//适用门店
                settleType: '1',//收款方式：1-总店收款 2-分店收款 3-区域收款
                showChannel: '1',//投放渠道 1-不限制 2-仅直播间售卖
                soldEndTime: '',//售卖结束时间 yyyy-MM-dd
                soldStartTime: '',//售卖开始时间 yyyy-MM-dd
                soldTimeType: '1',//售卖时间类型 1 限时售卖
                stockCount: '',//总库存
                stockLimitType: '2',//库存限制类型 1 有限库存 2 无限库存
                superimposedDiscountsType: '2',//可与店内优惠同享 1 同享 2 不同享
                useDateDay: '',//指定天数 大于0 小于360
                useDateType: '2',//使用天数限制类型 1 指定日期 2 指定天数
                useEndDate: '',//yyyy-MM-dd 结束日期，use_date_type=1时有效
                useLimitAmount: '',//限制使用张数
                useLimitType: '1',//限制使用类型 1 不限制 2 限制
                useNotDateList: [],//不可使用时间（指定不可用日期数组）
                useNotDateType: '2',//不可使用时间开关 1开启 2关闭
                useNotWeek: [],//每周几不可用 [1,2,3]
                useNotWeekType: '2',//不可使用周开关 1开启 2关闭
                useStartDate: '',//yyyy-MM-dd 开始日期，use_date_type=1时有效
                useTimeList: [ //每日可用时间段（仅指定时间可用数组）
                    {
                        endTimeIsNextDay: '1',//是否跨天 1跨天 2 不跨天
                        useEndTime: '',//结束时间 00:00:00
                        useStartTime: '',//开始时间 00:00:00
                    }
                ],
                useTimeType: '1',//使用时间类型 1 营业时间可用 2 指定时间段
                
                categoryValue: [],//品类数组
                diningNum: '1',//用餐人数 5是自定义
                numPeopleMin:'1',//人数范围最小
                numPeopleMax:'2',//人数范围最大
                dateSales:[],//售卖日期
                appointDate:[],//指定日期
            },
            rules: { //校验规则
                
            },
            commodityImg: [], //商品相册
            imgTxtls: [], //图文详情
            id: '', //获取详情id
            info: {}, //获取详情
            endDisabled: true,
            useEndDisabled: true,
            goodsList: [],
            tabelList: [],
            radioList: [
                { id: '1', value: '统一收款', tips: '总店账户统一接收该团购产生的所有账款' },
                { id: '3', value: '区城账户收款', tips: '区域账户统一接收关联门店产生的所有账款' },
                { id: '2', value: '分店收款', tips: '各门店产生的账款分别由各自门店账户进行按收' },
            ],
            numPeopleOptionOne: peopleLsOne,
            numPeopleOptionTwo: peopleLsTwo,
            merchantLs: [],//所属商户
            categoryOption: [],//商品品类
            commodityTypeOption:[{ id: '1', value: '团购券' }], //商品类型
            unitOptions:[
                {id:'份',value:'份'}
            ],
            numSelectLs:[{id:'',value:'全部可选'}],
            weekOptions:[
                {id:1,value:'周一'},{id:2,value:'周二'},{id:3,value:'周三'},{id:4,value:'周四'},{id:5,value:'周五'},
                {id:6,value:'周六'},{id:7,value:'周日'},
            ],
            timeTypeOption:[
                {id:'1',value:'天'},{id:'2',value:'小时'},{id:'3',value:'分钟'},
            ],
            type:'',//判断是否是复制创建
        };
    },
    computed: {
        // 计算商品搭配单品个数
        countingSingleItems() {
            var num = 0
            this.ruleForm.goodsGroupList.forEach(res => {
                num += (res.itemList.length ? res.itemList.length : 0)
            })
            return num
        }
    },
    created() { },
    mounted() {
        let query = this.$route.query;
        this.id = query.id
        this.type = query.type
        this.getselectList()
    },
    methods: {
        nextStep() { //下一步
            this.step = 2
        },
        backStep(){ //上一步
            this.step = 1 
        },
        getselectList() { //获取所属商户列表
            douyinMerchant()
                .then((res) => {
                    this.merchantLs = res.result;
                    if (this.id != undefined) {
                        this.getInfo()
                    }
                })
        },
        dyShopChange(e){ //商户选择
            this.getDouyinCategory(e)
            this.getDouyinPoi(e)
        },
        getDouyinCategory(id){ //获取类目
            let params = {
                dyShopAccountId: id
            }
            douyinCategory(params)
                .then((res) => {
                    this.categoryOption = res.result;
                    this.categoryOption.forEach((item) => {
                        item.children.forEach((items) => {
                            items.children.forEach((itemss) => {
                                if (itemss.children.length == 0) {
                                    itemss.children = null
                                }
                            })
                        })
                    })
                })
        },
        changeCategory(){ //类目选择
            console.log(this.ruleForm.categoryValue)
            this.ruleForm.categoryIdOne = this.ruleForm.categoryValue[0]
            this.ruleForm.categoryIdTwo = this.ruleForm.categoryValue[1]
            this.ruleForm.categoryIdThree = this.ruleForm.categoryValue[2]
        },
        getDouyinPoi(id){ //获取适用门店
            let params = {
                dyShopAccountId: id
            }
            douyinPoi(params)
                .then((res) => {
                    this.goodsList = res.result;
                    if(this.id!=undefined){
                        this.tabelListGet()
                    }
                })
        },
        //选择
        changePunchActivityId(e, index) {
            // this.goodsNum = e.length
            this.tabelList = []
            this.ruleForm.shopList.forEach(item => {
                this.goodsList.forEach(aitem => {
                    if (item == aitem.id) {
                        this.tabelList.push(aitem)
                    }
                })
            })
            let settleType = ''
            if(this.ruleForm.settleType=='2'){
                settleType = '分店收款'
            }else if(this.ruleForm.settleType=='3'){
                settleType = '区城账户收款'
            }else{
                settleType = '统一收款'
            }
            this.tabelList.forEach((item)=>{
                item.settleType = settleType
            })
        },
        //收款方式选择
        settleTypeChange(){
            let settleType = ''
            if(this.tabelList.length!=0){
                this.tabelList = []
                this.ruleForm.shopList.forEach(item => {
                this.goodsList.forEach(aitem => {
                        if (item == aitem.id) {
                            this.tabelList.push(aitem)
                        }   
                    })
                })
               if(this.ruleForm.settleType=='2'){
                    settleType = '分店收款'
                }else if(this.ruleForm.settleType=='3'){
                    settleType = '区城账户收款'
                }else{
                    settleType = '统一收款'
                }
                this.tabelList.forEach((item)=>{
                    item.settleType = settleType
                })    
            }
        },
        //套餐图片
        getSetMealImgList(e) {
            console.log(e)
            if (e != '') {
                this.ruleForm.setMealImgList = e.split(",")
            } else {
                this.ruleForm.setMealImgList = []
            }
            console.log(this.ruleForm.setMealImgList)
            this.$refs['setMealImgRef'].clearValidate();
        },
        //菜品图
        getDishesImgList(e) {
            if (e != '') {
                this.ruleForm.dishesImgList = e.split(",")
            } else {
                this.ruleForm.dishesImgList = []
            }
        },
        //环境图
        getCircumstancesImgList(e) {
            if (e != '') {
                this.ruleForm.circumstancesImgList = e.split(",")
            } else {
                this.ruleForm.circumstancesImgList = []
            }
        },
        //添加时段
        addTime() {
            let useTimeList = {
                endTimeIsNextDay: '',//是否跨天 1跨天 2 不跨天
                useEndTime: '',//结束时间 00:00:00
                useStartTime: '',//开始时间 00:00:00
            }
            this.ruleForm.useTimeList.push(useTimeList)
        },
        //删除时段
        deleteTime(index){
            this.ruleForm.useTimeList.splice(index, 1)
        },
        //可用日期类型选择
        useDateTypeChange(){
            this.$refs['dateRef'].clearValidate();
        },
        //
        useNotWeekTypeChange(){
            this.$refs['useNotWeekRef'].clearValidate();
        },
        //判断是否有回车字符
        containsNewLineAndSplit(str) {
            const hasNewLine = /\n/g.test(str); // 检查是否包含回车符
            if (hasNewLine) {
                return str.split(/\n/g); // 使用正则表达式作为分隔符分割字符串
            }
            return null; // 如果不包含回车符，返回null
        },
        //几选几
        numSelectIdChange(e){
            this.ruleForm.goodsGroupList.forEach((item)=>{
                item.totalCount = this.countingSingleItems
                item.optionCount = e
            })
            this.getPrice()
            this.$forceUpdate()
        },
        //划线价计算
        getPrice(){
            let sum = 0
            this.ruleForm.goodsGroupList.forEach(item => {
                let arr = []
                let arr2 = []
                item.itemList.forEach(nav => {
                    arr.push(Number(nav.price))
                    arr2 = this.findTwoLargestValues(arr,item.numSelectId)
                });
                for (let i = 0; i < arr2.length; i++) {  
                    sum += arr2[i];  
                }  
            });
            this.ruleForm.originAmount = sum
        },
        //寻找最大值
        findTwoLargestValues(array,num){
            array.sort(function(a, b) {
                return b - a;
            });
            // 返回数组中的最大多个值
            return array.slice(0, num);
        },
        //
        priceInput(){
            this.getPrice()
        },
        getInfo() { //获取商家团购信息详情
            goodsDetail(this.id).then((res) => {
                this.loading = true;
                this.auditStatus = res.result.auditStatus
                this.getDouyinCategory(res.result.dyShopAccountId)
                this.getDouyinPoi(res.result.dyShopAccountId)
                setTimeout(()=>{
                    this.ruleForm = {
                        actualAmount: res.result.actualAmount,//顾客实际支付价格 单位:元
                        appointmentDateType: res.result.appointmentDateType,//预约时间类型 1 天 2 小时 3 分钟
                        appointmentNum: res.result.appointmentNum,//预约时间数
                        appointmentType: res.result.appointmentType,//预约开关 1 开启 2关闭
                        buyLimitAmount: res.result.buyLimitAmount,//限购数量
                        buyLimitType: res.result.buyLimitType,//限购类型 1 限购 2 不限购
                        categoryIdOne: res.result.categoryIdOne,//一级类目id
                        categoryIdThree: res.result.categoryIdThree,//三级类目id
                        categoryIdTwo: res.result.categoryIdTwo,//二级类目id
                        circumstancesImgList: res.result.circumstancesImgList,//环境图
                        dishesImgList: res.result.dishesImgList,//菜品图
                        dyShopAccountId: res.result.dyShopAccountId,//抖音生活服务商家账户id
                        freePackType: res.result.freePackType,//堂食外带约定 1仅堂食 2仅外带 3支持堂食和外带
                        freebieExchangeRule: res.result.freebieExchangeRule,//赠品兑换规则
                        freebieName: res.result.freebieName,//赠品名称
                        freebieStockCount: res.result.freebieStockCount,//赠品库存总量
                        freebieStockType: res.result.freebieStockType,//赠品库存类型 1有库存 2无库存
                        freebieType: res.result.freebieType,//赠品包含类型 1包含 2不包含
                        goodsCollocationType: res.result.goodsCollocationType,//商品搭配类型 1 设置单品价格，自动计算划线价 2 设置划线价，无需设置单品价格
                        goodsGroupList: [ //商品搭配
                            {
                                groupName: '',//组名
                                optionCount: '',//菜品选择数量
                                totalCount: '',//商品组菜单总数
                                numSelectId:'',//几选几
                                itemList: [
                                    {
                                        name: '',//菜名
                                        count: '',//总数
                                        unit: '份',//单位 例：“份”
                                        price: '',//单价
                                    }
                                ],
                            }
                        ],
                        id: res.result.id,
                        noteList: res.result.noteList,//其他说明
                        noteListStr: res.result.noteList.toString(),//其他说明字符串
                        originAmount: res.result.originAmount,//商品原价（商品划线价）单位：元
                        privateRoomType: res.result.privateRoomType,//包间使用类型 1 使用 2不使用
                        productName: res.result.productName,//商品名称
                        productType: res.result.productType,//商品类型 1 团购券
                        recPersonNum: res.result.recPersonNum,//建议用餐人数
                        recPersonNumMax: res.result.recPersonNumMax,//建议用餐最大人数
                        setMealImgList: res.result.setMealImgList,//套餐图
                        shopList: res.result.shopList,//适用门店
                        settleType: res.result.settleType,//收款方式：1-总店收款 2-分店收款 3-区域收款
                        showChannel: res.result.showChannel,//投放渠道 1-不限制 2-仅直播间售卖
                        soldEndTime: res.result.soldEndTime,//售卖结束时间 yyyy-MM-dd
                        soldStartTime: res.result.soldStartTime,//售卖开始时间 yyyy-MM-dd
                        soldTimeType: res.result.soldTimeType,//售卖时间类型 1 限时售卖
                        stockCount: res.result.stockCount,//总库存
                        stockLimitType: res.result.stockLimitType,//库存限制类型 1 有限库存 2 无限库存
                        superimposedDiscountsType: res.result.superimposedDiscountsType,//可与店内优惠同享 1 同享 2 不同享
                        useDateDay: res.result.useDateDay,//指定天数 大于0 小于360
                        useDateType: res.result.useDateType,//使用天数限制类型 1 指定日期 2 指定天数
                        useEndDate: res.result.useEndDate,//yyyy-MM-dd 结束日期，use_date_type=1时有效
                        useLimitAmount: res.result.useLimitAmount,//限制使用张数
                        useLimitType: res.result.useLimitType,//限制使用类型 1 不限制 2 限制
                        useNotDateList: res.result.useNotDateList,//不可使用时间（指定不可用日期数组）
                        useNotDateType: res.result.useNotDateType,//不可使用时间开关 1开启 2关闭
                        useNotWeek: res.result.useNotWeek,//每周几不可用 [1,2,3]
                        useNotWeekType: res.result.useNotWeekType,//不可使用周开关 1开启 2关闭
                        useStartDate: res.result.useStartDate,//yyyy-MM-dd 开始日期，use_date_type=1时有效
                        useTimeList: res.result.useTimeList,
                        useTimeType: res.result.useTimeType,//使用时间类型 1 营业时间可用 2 指定时间段
                        
                        categoryValue: [res.result.categoryIdOne,res.result.categoryIdTwo,res.result.categoryIdThree],//品类数组
                        diningNum: '1',//用餐人数 5是自定义
                        numPeopleMin:'1',//人数范围最小
                        numPeopleMax:'2',//人数范围最大
                        dateSales:[],//售卖日期
                        appointDate:[],//指定日期
                    }
                    this.ruleForm.goodsGroupList = res.result.goodsGroupList
                    this.ruleForm.goodsGroupList.forEach((item)=>{
                        item.numSelectId = item.optionCount
                    })
                    //用餐人数处理
                    if((this.ruleForm.recPersonNum==this.ruleForm.recPersonNumMax)&&this.ruleForm.recPersonNumMax<=4){
                        this.ruleForm.diningNum = String(this.ruleForm.recPersonNum)
                    }else{
                        this.ruleForm.diningNum = '5'
                        this.ruleForm.numPeopleMin = this.ruleForm.recPersonNum
                        this.ruleForm.numPeopleMax = this.ruleForm.recPersonNumMax
                    }
                    //售卖日期处理
                    this.ruleForm.dateSales = [this.ruleForm.soldStartTime,this.ruleForm.soldEndTime]
                    //指定日期处理
                    this.ruleForm.appointDate = [this.ruleForm.useStartDate,this.ruleForm.useEndDate]
                    //其他说明信息处理
                    if(res.result.noteList.length>1){
                        this.ruleForm.noteListStr = this.ruleForm.noteListStr.replace(/,/g, '\n');
                    }
                    this.loading = false;
                },100)
            }).catch(()=>{
                this.loading = false;
            })
        },
        //表格处理
        tabelListGet(){
            this.tabelList = []
            this.ruleForm.shopList.forEach(item => {
                this.goodsList.forEach(aitem => {
                    if (item == aitem.id) {
                        this.tabelList.push(aitem)
                    }
                })
            })
            let settleType = ''
            if(this.ruleForm.settleType=='2'){
                settleType = '分店收款'
            }else if(this.ruleForm.settleType=='3'){
                settleType = '区城账户收款'
            }else{
                settleType = '统一收款'
            }
            this.tabelList.forEach((item)=>{
                item.settleType = settleType
            })         
        },
        //提交审核
        submitForm(formName,status) {
            //建议用餐人数数据处理
            if(this.ruleForm.diningNum=='1'){
                this.ruleForm.recPersonNum = 1
                this.ruleForm.recPersonNumMax = 1
            } else if(this.ruleForm.diningNum=='2'){
                this.ruleForm.recPersonNum = 2
                this.ruleForm.recPersonNumMax = 2
            } else if(this.ruleForm.diningNum=='3'){
                this.ruleForm.recPersonNum = 3
                this.ruleForm.recPersonNumMax = 3
            } else if(this.ruleForm.diningNum=='4'){
                this.ruleForm.recPersonNum = 4
                this.ruleForm.recPersonNumMax = 4
            }else{
                this.ruleForm.recPersonNum = this.ruleForm.numPeopleMin
                this.ruleForm.recPersonNumMax = this.ruleForm.numPeopleMax
            }
            //售卖日期时间处理
            if(this.ruleForm.dateSales.length!=0){
                this.ruleForm.soldStartTime = this.ruleForm.dateSales[0]
                this.ruleForm.soldEndTime = this.ruleForm.dateSales[1]
            }
            //可用日期（指定日期时间处理）
            if (this.ruleForm.appointDate.length!=0) {
                this.ruleForm.useStartDate = this.ruleForm.appointDate[0]
                this.ruleForm.useEndDate = this.ruleForm.appointDate[1]
            }
            //其他说明信息处理
            const strResult = this.containsNewLineAndSplit(this.ruleForm.noteListStr);
            if(strResult==null){
                this.ruleForm.noteList = [this.ruleForm.noteListStr]
            }else{
                this.ruleForm.noteList = strResult
            }
            console.log('this.ruleForm',this.ruleForm)
            // return
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let data = {
                       ...this.ruleForm 
                    }
                    this.sumitLoading = true
                    if(this.id!=undefined&&this.type!='copy'){ //编辑
                        if(status==1){ //提交审核
                            editGoods(data)
                            .then((res) => {
                                this.$message({
                                    message: "修改成功",
                                    type: "success",
                                });
                                setTimeout(() => {
                                    this.$router.push({
                                        path: "/dyGroupBuying",
                                    });
                                    this.sumitLoading = false
                                }, 1000);
                            }).catch(()=>{
                                this.sumitLoading = false
                            })
                        }else{ //草稿
                            editGoodsDraft(data)
                            .then((res) => {
                                this.$message({
                                    message: "修改成功",
                                    type: "success",
                                });
                                setTimeout(() => {
                                    this.$router.push({
                                         path: "/dyGroupBuying",
                                    });
                                    this.sumitLoading = false
                                }, 1000);
                            }).catch(()=>{
                                this.sumitLoading = false
                            })
                        }
                    }else{ //新增
                        if(status==1){ //提交审核
                            saveTradeGoods(data)
                            .then((res) => {
                                this.$message({
                                    message: "操作成功",
                                    type: "success",
                                });
                                setTimeout(() => {
                                    this.$router.push({
                                        path: "/dyGroupBuying",
                                    });
                                    this.sumitLoading = false
                                }, 1000);
                            }).catch(()=>{
                                this.sumitLoading = false
                            })
                        }else{ //草稿
                            saveTradeGoodsDraft(data)
                            .then((res) => {
                                this.$message({
                                    message: "操作成功",
                                    type: "success",
                                });
                                setTimeout(() => {
                                    this.$router.push({
                                         path: "/dyGroupBuying",
                                    });
                                    this.sumitLoading = false
                                }, 1000);
                            }).catch(()=>{
                                this.sumitLoading = false
                            })
                        }                           
                    }
                    
                }else{
                    console.log('请完善信息')
                }
            });
        },
        resetForm(formName) {
            //重置
            this.$refs[formName].resetFields();
        },
        addProduct() {
            //添加产品
            this.ruleForm.productLs.push({
                productName: "", //产品名称
                productNum: "", //产品数量
            });
        },
        deleteProduct(index) {
            //删除产品
            this.ruleForm.productLs.splice(index, 1);
        },
        getImgLs(e) {
            //商品相册
            // this.commodityImg = e;
            this.ruleForm.projectList = e
        },
        getimgTxtls(e) {
            //图文详情
            // this.imgTxtls = e;
            this.ruleForm.detailList = e
        },
        // 添加新组
        addGroup() {
            let goodsGroupList = {
                groupName: '',//组名
                optionCount: '',//菜品选择数量
                totalCount: '',//商品组菜单总数
                numSelectId:'',//几选几
                itemList: [
                    {
                        name: '',//菜名
                        count: '',//总数
                        unit: '份',//单位 例：“份”
                        price: '',//单价
                    }
                ],
            }
            this.ruleForm.goodsGroupList.push(goodsGroupList)
        },
        // 添加单品
        addTag(index) {
            let shopItem = {
                name: '',//菜名
                count: '',//总数
                unit: '份',//单位 例：“份”
                price: '',//单价
            }
            console.log(this.ruleForm.goodsGroupList[index])
            this.ruleForm.goodsGroupList[index].itemList.push(shopItem)
            this.ruleForm.goodsGroupList[index].numSelectId = ''
        },
        //删除组
        deleteGroup(index) {
            this.ruleForm.goodsGroupList.splice(index, 1)
        },
        //删除单品
        deleteTag(fatherIndex, sonIndex) {
            this.ruleForm.goodsGroupList[fatherIndex].itemList.splice(sonIndex, 1)
            this.ruleForm.goodsGroupList[fatherIndex].numSelectId = ''
        },
        //点击上移
        clickUp(index) {
            this.swapArray(this.ruleForm.goodsGroupList, index - 1, index);
        },
        //点击下移
        clickDown(index) {
            this.swapArray(this.ruleForm.goodsGroupList, index, index + 1);
        },
        //数组元素互换位置
        swapArray(arr, index1, index2) {
            arr[index1] = arr.splice(index2, 1, arr[index1])[0];
            return arr;
        },
    },
};

</script>
<style lang="scss" scoped>
.main {
    background-color: #fff;
    border-radius: 8px;
    padding: 1px 20px;
}

.main_info {
    margin-top: 40px;
}

.title_box {
    color: #1890ff;
    font-weight: bold;
    margin-bottom: 20px;
}

.radio_box {
    margin-top: 12px;
}

.radio_box_tips {
    font-size: 12px;
    color: #ccc !important;
    margin-top: 10px;
}

::v-deep.radio_box .el-radio {
    display: flex;
}

.margin_box {
    margin-bottom: 10px;
}

.from_detail_box {
    width: 800px;
    background-color: #f8fbff;
    padding: 10px;
    margin-bottom: 5px;
}

.from_detail_box_son {
    margin-bottom: 2px
}

.son_right {
    margin-right: 10px;
}

.tips_txt {
   
    font-size: 12px;
    color: #9f9e9e;
}

.operate_box {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.from_detail_bottom {
    background-color: #f8fbff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 800px;
    margin-top: 20px;
    padding: 10px;
}



.stock_txt {
    font-size: 13px;
    color: #333333;
    padding-left: 100px;
    margin-bottom: 20px;
}

.dflex {
    display: flex;
    align-items: center;
}

.el_table_box {
    padding: 0 20px 40px;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    margin-left: 100px;
    margin-bottom: 20px;
}

.el_table_box_h {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid #f3f3f3;
    margin-bottom: 14px;
}

.el_table_box_h_title {
    border-left: 4px solid #ff9300;
    padding-left: 4px;
}

.commodity_album {
    padding-top: 20px;
}

.carousel_box {
    padding-top: 60px;
    width: 400px;
}

.carousel_box_title {
    font-size: 15px;
    // color: #333333;
    color: #ff9300;
    margin-bottom: 20px;
}

.swiper_img {
    width: 100%;
    height: 100%;
}

.phone_box {
    border: 10px solid #f3f3f3;
    border-radius: 8px;
    line-height: 0;
}

.button_box {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 30px 0 30px 100px;
    width: 70%;
}

.explain_txt {
    font-size: 20px;
    margin-left: 20px;
    color: #ff9300;
}

.el_table_box_d {
    border: 1px solid #dfe6ec;
    width: 100%;
}

.el_table_box_d_t {
    display: flex;
    align-items: center;
    color: #909399;
    font-size: 15px;
    font-weight: bold;
}

.tab_index {
    width: 64px;
    padding: 10px;
    border-right: 1px solid #dfe6ec;
    height: 56px;
    display: flex;
    align-items: center;
}

.tab_name {
    height: 56px;
    display: flex;
    align-items: center;
    flex: 1;
    padding: 10px;
    border-right: 1px solid #dfe6ec;
}

.tab_num {
    height: 56px;
    display: flex;
    align-items: center;
    width: 35%;
    padding: 10px;
    border-right: 1px solid #dfe6ec;
}

.tab_operation {
    width: 160px;
    padding: 10px;
    height: 56px;
    display: flex;
    align-items: center;
}

.tab_index_d {
    width: 64px;
    padding: 10px;
    border-right: 1px solid #dfe6ec;
    height: 76px;
    display: flex;
    align-items: center;
}

.tab_name_d {
    height: 76px;
    display: flex;
    align-items: center;
    flex: 1;
    padding: 10px;
    border-right: 1px solid #dfe6ec;
}

.tab_num_d {
    height: 76px;
    display: flex;
    align-items: center;
    width: 35%;
    padding: 10px;
    border-right: 1px solid #dfe6ec;
}

.tab_operation_d {
    width: 160px;
    padding: 10px;
    height: 76px;
    display: flex;
    align-items: center;
}

.el_table_box_d_b {
    display: flex;
    align-items: center;
    color: #666;
    font-size: 15px;
    border-top: 1px solid #dfe6ec;
}

.detail_button {
    color: #ff4949;
    font-size: 14px;
    cursor: pointer;
}

.tab_none {
    color: #909399;
    font-size: 15px;
    border-top: 1px solid #dfe6ec;
    width: 100%;
    text-align: center;
    padding: 20px 0;
}

.disx {
    display: flex;
    align-items: center;
}
.img_box{
    width: 120px;
    margin-right: 10px;
}
::v-deep .el-textarea.is-disabled .el-textarea__inner {
    background-color: #f8fbff;
    border-color: #FFFFFF;
    color: #000000;
}
</style>